﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;
using System.Net;

namespace ApproverTools_v2
{
    public partial class FormMain : Form
    {

        #region Members

        DataTable _tbl = new DataTable("tbl");
        DataTable _tblFiles = new DataTable("files");
        DataGridViewRow _selrow = null;
        int ApproveStep = 0;

        #endregion

        #region constructors
        public FormMain()
        {
            InitializeComponent();
        }
        #endregion

        #region form methods
        private void FormMain_Load(object sender, EventArgs e)
        {
        }
        #region form buttons
        private void выходИзПрограммыToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        #region Print Btns
        private void btnPrintPageProp_Click(object sender, EventArgs e)
        {
            oBrowser.ShowPageSetupDialog();
        }
        private void btnPrintPreview_Click(object sender, EventArgs e)
        {
            oBrowser.ShowPrintPreviewDialog();
        }
        private void btnPrint_Click(object sender, EventArgs e)
        {
            oBrowser.ShowPrintDialog();
        }
        #endregion
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            _gv.DataSource = null;
            _tbl = null;
            DataTable tbl = null;
            oMsg.Text = "Получение данных";
            Exception err = GetDataList_x32(ref tbl);
            if (err != null)
            {
                MessageBox.Show(err.Message);
            }
            else if(tbl != null && tbl.Rows.Count>0)
            {
                _tbl = tbl;
                _gv.DataSource = _tbl;

                DataColumn col = new DataColumn("IsCheck", typeof(bool));
                _tbl.Columns.Add(col);

                DataColumn col2 = new DataColumn("IsApprove", typeof(bool));
                _tbl.Columns.Add(col2);

                _gv.Columns[0].DisplayIndex = _gv.Columns.Count - 2;
                _gv.Columns[_gv.Columns.Count - 2].DisplayIndex = 0;

                _gv.Columns[1].DisplayIndex = _gv.Columns.Count - 1;
                _gv.Columns[_gv.Columns.Count - 1].DisplayIndex = 1;

                _gv.Columns["_x0414__x0430__x0442__x0430__x0020__x043d__x0430__x043a__x043b__x0430__x0434__x043d__x043e__x0439_"].HeaderText = "Дата накладной";
                _gv.Columns["_x041d__x043e__x043c__x0435__x0440__x0020__x043d__x0430__x043a__x043b__x0430__x0434__x043d__x043e__x0439_"].HeaderText = "Номер накладной";
                _gv.Columns["Shop"].HeaderText = "Магазины";
                _gv.Columns["Supplier"].HeaderText = "Поставщик";
                _gv.Columns["_x0414__x0430__x0442__x0430__x0020__x043a__x043e__x043c__x043f__x043b__x0435__x043a__x0442__x0430_"].HeaderText = "Дата комплекта";
                _gv.Columns["Title"].HeaderText = "Номер";
                _gv.Columns["_x0421__x043e__x0441__x0442__x043e__x044f__x043d__x0438__x0435__x0020__x0443__x0442__x0432__x0435__x0440__x0436__x0434__x0435__x043d__x0438__x044f_"].HeaderText = "Состояние";

                foreach (DataGridViewColumn c in _gv.Columns)
                    c.ReadOnly = true;
                _gv.Columns[_gv.Columns.Count - 2].ReadOnly = false;
                _gv.Columns[_gv.Columns.Count - 2].HeaderText = "Выбран";
                _gv.Columns[_gv.Columns.Count - 2].Width = 54;

                _gv.Columns[_gv.Columns.Count - 1].ReadOnly = true;
                _gv.Columns[_gv.Columns.Count - 1].HeaderText = "Утвержден";
                _gv.Columns[_gv.Columns.Count - 1].Width = 65;

            }
            oMsg.Text = "";
        }
        #region Check
        private void btnCheck_Click(object sender, EventArgs e)
        {
            _gv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            if (_tbl != null)
            {
                foreach (DataRow r in _tbl.Rows)
                    r["IsCheck"] = true;
            }
        }
        private void btnUnCheck_Click(object sender, EventArgs e)
        {
            _gv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            if (_tbl != null)
            {
                foreach (DataRow r in _tbl.Rows)
                    r["IsCheck"] = false;
            }
        }
        #endregion
        private void _gv_MouseClick(object sender, MouseEventArgs e)
        {
            _gv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                contextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
            }
        }
        private void открытьДокуиентВБраузереToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _gv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            ViewInBrowser();
        }
        private void утвердитьДокументToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _gv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            Approve();
        }
        private void утвердитьToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _gv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            System.Threading.Thread.Sleep(50);
            ApproveWinForms();
        }
        private void btnFilesSaveAll_Click(object sender, EventArgs e)
        {
            _gv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            SaveAllFiles();
        }
        private void файлыToolStripMenuItem_Click(object sender, EventArgs e)
        {
        
            _gv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            if (_gv.SelectedRows.Count <= 0)
                return;
            _selrow = _gv.SelectedRows[0];
            if(_selrow != null)
            {
                DataTable tblTmp = null;
                List<string> errList = new List<string>();
                int id = (int)_selrow.Cells["ID"].Value;
                Exception err = GetSP_PermDocs_FilesList_ByDocID(ref tblTmp, ref errList, id);
                if (err == null)
                {
                    _tblFiles = tblTmp;
                    _gvFiles.DataSource = _tblFiles;
                    tabControl1.SelectedTab = tabFiles;
                }
                else
                    MessageBox.Show(err.Message);
            }
        }
        #endregion
        #region tab events
        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {            
        }
        private void tabData_Enter(object sender, EventArgs e)
        {
            ApproveStep = 0;
        }
        private void tabFiles_Leave(object sender, EventArgs e)
        {
            ApproveStep = 0;
        }
        private void tabBrowser_Leave(object sender, EventArgs e)
        {
            _gv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            oBrowser.Navigate(@"http://sp.korablik.local/shops/_layouts/IMAGES/GEARS_AN.GIF");
            //oBrowser.GoHome();
        }
        private void tabApprovePanel_Enter(object sender, EventArgs e)
        {
            _gv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            System.Threading.Thread.Sleep(50);
            InvalidateApprovePanel();
        }
        private void tabApprovePanel_Leave(object sender, EventArgs e)
        {
            InvalidateApprovePanel();
        }
        #endregion
        #region grid view methods
        private void _gv_DoubleClick(object sender, EventArgs e)
        {
            _gv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            ViewInBrowser();
        }
        private void _gvFiles_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            _gv.CommitEdit(DataGridViewDataErrorContexts.Commit);

            ViewInBrowser_File();
        }
        #endregion
        #endregion

        #region Common
        void ViewInBrowser()
        {
            if (_gv.SelectedRows.Count <= 0)
                return;

            _selrow = _gv.SelectedRows[0];

            string UrlStr = @"http://sp.korablik.local/shops/DocLib/"
                + _selrow.Cells["Title"].Value + @"/?IsDlg=1";
            oBrowser.Navigate(UrlStr);

            tabControl1.SelectedTab = tabBrowser;
        }
        void ViewInBrowser_File()
        {
            if (_gvFiles.SelectedRows.Count <= 0)
                return;

            _selrow = _gvFiles.SelectedRows[0];

            string UrlStr = @"http://sp.korablik.local/shops/"
                + _selrow.Cells["url"].Value;
            oBrowser.Navigate(UrlStr);

            tabControl1.SelectedTab = tabBrowser;
        }
        void Approve()
        {
            if (_gv.SelectedRows.Count <= 0)
                return;

            _selrow = _gv.SelectedRows[0];

            string UrlStr =
@"http://sp.korablik.local/shops/_layouts/listform.aspx?PageType=6&ListId={CD03D74B-1D72-4702-A88C-22E262E624FB}&ID="
+ _selrow.Cells["ID"].Value + @"&ContentTypeID=0x0120D520003956F8DEB25BA34385A727CE68F1F3ED&RootFolder=%2Fshops%2FDocLib&IsDlg=1";

            tabControl1.SelectedTab = tabBrowser;
            System.Threading.Thread.Sleep(50);
            ApproveStep = 1;
            oBrowser.Navigate(UrlStr);
        }
        void ApproveWinForms()
        {
            _gv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            if (_gv.SelectedRows.Count <= 0)
                return;
            _selrow = _gv.SelectedRows[0];
            tabControl1.SelectedTab = tabApprove;
            System.Threading.Thread.Sleep(50);
            tabApprove.Focus();
            System.Threading.Thread.Sleep(50);
            InvalidateApprovePanel();            
        }
        void SaveAllFiles()
        {
            Exception ret = null;
            if (_tbl == null || _tbl.Rows.Count == 0)
                return;
            _fbd.Description = @"Путь для сохраения файлов";
            _fbd.ShowNewFolderButton = true;
            //folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal;
            if (_fbd.ShowDialog() == DialogResult.OK)
            {
                string pathStr = _fbd.SelectedPath;
                int id = -1;
                try
                {
                    foreach (DataRow r in _tbl.Rows)
                    {
                        if (r["ID"] == DBNull.Value)
                            continue;
                        if (r["IsCheck"] == DBNull.Value)
                            continue;
                        if ((bool)r["IsCheck"] == false)
                            continue;

                        id = (int)r["ID"]; // sample - 384940
                        DataTable tblTmp = new DataTable("tblTmp");
                        List<string> errList = new List<string>();

                        try
                        {
                            Exception err = GetSP_PermDocs_FilesList_ByDocID(ref tblTmp, ref errList, id);
                            if (err == null && tblTmp != null)
                            {
                                foreach (DataRow rr in tblTmp.Rows)
                                {
                                    string strTmp = (string)r["_x041c__x0430__x0433__x0430__x0437__x0438__x043d_"];
                                    int i = strTmp.IndexOf('|');
                                    string shopname = strTmp.Substring(0, i);
                                    string filename =
                                        pathStr + @"\"
                                        + shopname
                                        + "_"
                                        + r["Title"]
                                        + "_"
                                        + rr["FileName"];

                                    string filePathSrv =
@"http://sp.korablik.local:98/ws/SP/PermDocs_FileData.aspx?intID=" 
+ id + "&FileName=" + rr["FileName"] + @"&guid=" + Guid.NewGuid().ToString();

                                    WebClient wc = new WebClient();
                                    byte[] srvdata = wc.DownloadData(filePathSrv);
                                    using (FileStream filestream =
                                        File.Create(filename, 1024, FileOptions.None))
                                    {
                                        filestream.Write(srvdata, 0, srvdata.Length);
                                    }

                                }
                            }
                        }
                        catch (Exception err)
                        {
                            ret = new Exception(err.Message, err);
                        }
                    }
                }
                catch (Exception err)
                {
                    ret = new Exception(err.Message, err);
                }
            }
        }
        void InvalidateApprovePanel()
        {
            _gv.CommitEdit(DataGridViewDataErrorContexts.Commit);
            System.Threading.Thread.Sleep(50);
            btnSave.Enabled = false;

            oDocDateNakl.Text = "";
            oDocNoNakl.Text = "";
            oShop.Text = "";
            oSupplier.Text = "";
            oDocNo.Text = "";
            oDocDate.Text = "";

            if (_selrow == null || _selrow.Index == -1)
                return;

            int i = 0;

            DateTime dtDocNakl;
            if (_selrow.Cells["_x0414__x0430__x0442__x0430__x0020__x043d__x0430__x043a__x043b__x0430__x0434__x043d__x043e__x0439_"].Value != DBNull.Value)
            {
                dtDocNakl = (DateTime)_selrow.Cells["_x0414__x0430__x0442__x0430__x0020__x043d__x0430__x043a__x043b__x0430__x0434__x043d__x043e__x0439_"].Value;
                oDocDateNakl.Text = dtDocNakl.ToString();
            }

            string NoNakl = "";
            if (_selrow.Cells["_x041d__x043e__x043c__x0435__x0440__x0020__x043d__x0430__x043a__x043b__x0430__x0434__x043d__x043e__x0439_"].Value != DBNull.Value)
            {
                NoNakl = (string)_selrow.Cells["_x041d__x043e__x043c__x0435__x0440__x0020__x043d__x0430__x043a__x043b__x0430__x0434__x043d__x043e__x0439_"].Value;
                oDocNoNakl.Text = NoNakl;
            }

            string ShopName = "";
            if (_selrow.Cells["Shop"].Value != DBNull.Value)
            {
                ShopName = (string)_selrow.Cells["Shop"].Value;
                i = ShopName.IndexOf('|');
                if(i>=0)
                    ShopName = ShopName.Substring(0, i);
                oShop.Text = ShopName;
            }

            string SupplierName = "";
            if (_selrow.Cells["Supplier"].Value != DBNull.Value)
            {
                SupplierName = (string)_selrow.Cells["Supplier"].Value;
                i = SupplierName.IndexOf('|');
                if (i >= 0)
                    SupplierName = SupplierName.Substring(0, i);
                oSupplier.Text = SupplierName;
            }

            string DocNo = "";
            if (_selrow.Cells["Title"].Value != DBNull.Value)
            {
                DocNo = (string)_selrow.Cells["Title"].Value;
                oDocNo.Text = DocNo;
            }

            DateTime dtDocDate;
            if (_selrow.Cells["_x0414__x0430__x0442__x0430__x0020__x043a__x043e__x043c__x043f__x043b__x0435__x043a__x0442__x0430_"].Value != DBNull.Value)
            {
                dtDocDate = (DateTime)_selrow.Cells["_x0414__x0430__x0442__x0430__x0020__x043a__x043e__x043c__x043f__x043b__x0435__x043a__x0442__x0430_"].Value;
                oDocDate.Text = dtDocDate.ToString();
            }

            string DocStatus = "";
            if (_selrow.Cells["_x0421__x043e__x0441__x0442__x043e__x044f__x043d__x0438__x0435__x0020__x0443__x0442__x0432__x0435__x0440__x0436__x0434__x0435__x043d__x0438__x044f_"].Value != DBNull.Value)
            {
                DocStatus = (string)_selrow.Cells["_x0421__x043e__x0441__x0442__x043e__x044f__x043d__x0438__x0435__x0020__x0443__x0442__x0432__x0435__x0440__x0436__x0434__x0435__x043d__x0438__x044f_"].Value;
                if (DocStatus == "Черновик")
                    oDocStatus.SelectedIndex = 0;
                else if (DocStatus == "Ожидают утверждения")
                    oDocStatus.SelectedIndex = 1;
                else if (DocStatus == "Утвержден")
                    oDocStatus.SelectedIndex = 2;
            }

            tabApprove.Invalidate();

        }
        #endregion

        #region data
        Exception GetDataList_x32(ref DataTable parTpl)
        {
            Exception ret = null;
            List<string> errList = new List<string>();
            GetSP_PermDocs_ForApproverList(ref parTpl, ref errList);
            return ret;
        }
        Exception GetSP_PermDocs_ForApproverList(ref DataTable tbl, ref List<string> errList)
        {
            return Get(ref tbl, ref errList, @"http://sp.korablik.local:98/ws/SP/PermDocs_ForApproveList.aspx");
        }
        Exception GetSP_PermDocs_FilesList_ByDocID(ref DataTable tbl, ref List<string> errList, int parId)
        {
            string urlStr = @"http://sp.korablik.local:98/ws/SP/PermDocs_FilesInfo_ByDocSetID.aspx";
            return Get_ByID(ref tbl, ref errList, urlStr, parId);
        }
        Exception Get(ref DataTable tbl, ref List<string> errList, string UrlStr)
        {
            DateTime dt = DateTime.Now;
            dt = new DateTime(dt.Year, dt.Month, dt.Day);
            return Get(ref tbl, ref errList, UrlStr, dt.Year, dt.Month);
        }
        Exception Get(ref DataTable tbl, ref List<string> errList, string UrlStr, int Year, int Month)
        {
            Exception ret = null;
            try
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                wc.Headers["Cache-Control"] = "no-cache";
                wc.UseDefaultCredentials = false;
                wc.Credentials = new System.Net.NetworkCredential("anonymous", "janeDoe@contoso.com");
                wc.UseDefaultCredentials = false;

                string paramsStr = @"?guid=" + Guid.NewGuid().ToString()
                    + @"&year=" + Year.ToString()
                    + @"&month=" + Month.ToString();

                byte[] data = wc.DownloadData(UrlStr + paramsStr);
                // if (tbl == null)
                tbl = new DataTable();
                if (data.Length > 0)
                {
                    using (System.IO.MemoryStream xmlStream = new System.IO.MemoryStream())
                    {
                        xmlStream.Write(data, 0, data.Length);
                        xmlStream.Position = 0;
                        tbl.ReadXml(xmlStream);
                        xmlStream.Position = 0;
                        if (errList == null)
                            errList = new List<string>();
                        GetErrList(ref data, ref errList);
                        if (errList.Count > 0)
                            throw new Exception(errList[0]);
                    }
                }
                else
                    throw new Exception(@"Web Service http://sp.korablik.local не вернул данных");
            }
            catch (Exception err)
            {
                ret = new Exception(err.Message, err);
            }
            return ret;
        }
        Exception Get_ByID(ref DataTable tbl, ref List<string> errList, string UrlStr, int parID)
        {
            Exception ret = null;
            try
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                wc.Headers["Cache-Control"] = "no-cache";
                wc.UseDefaultCredentials = false;
                wc.Credentials = new System.Net.NetworkCredential("anonymous", "janeDoe@contoso.com");
                wc.UseDefaultCredentials = false;

                string paramsStr = @"?guid=" + Guid.NewGuid().ToString()
                    + @"&intID=" + parID;

                byte[] data = wc.DownloadData(UrlStr + paramsStr);
                // if (tbl == null)
                tbl = new DataTable();
                if (data.Length > 0)
                {
                    using (System.IO.MemoryStream xmlStream = new System.IO.MemoryStream())
                    {
                        xmlStream.Write(data, 0, data.Length);
                        xmlStream.Position = 0;
                        tbl.ReadXml(xmlStream);
                        xmlStream.Position = 0;
                        if (errList == null)
                            errList = new List<string>();
                        GetErrList(ref data, ref errList);
                        if (errList.Count > 0)
                            throw new Exception(errList[0]);
                    }
                }
                else
                    throw new Exception(@"Web Service http://sp.korablik.local не вернул данных");
            }
            catch (Exception err)
            {
                ret = new Exception(err.Message, err);
            }
            return ret;
        }
        Exception Get_ByID_And_Param(ref DataTable tbl, ref List<string> errList, string UrlStr, int parID, string par)
        {
            Exception ret = null;
            try
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                wc.Headers["Cache-Control"] = "no-cache";
                wc.UseDefaultCredentials = false;
                wc.Credentials = new System.Net.NetworkCredential("anonymous", "janeDoe@contoso.com");
                wc.UseDefaultCredentials = false;

                string paramsStr = @"?guid=" + Guid.NewGuid().ToString()
                    + @"&intID=" + parID
                    + par;

                byte[] data = wc.DownloadData(UrlStr + paramsStr);
                // if (tbl == null)
                tbl = new DataTable();
                if (data.Length > 0)
                {
                    using (System.IO.MemoryStream xmlStream = new System.IO.MemoryStream())
                    {
                        xmlStream.Write(data, 0, data.Length);
                        xmlStream.Position = 0;
                        tbl.ReadXml(xmlStream);
                        xmlStream.Position = 0;
                        if (errList == null)
                            errList = new List<string>();
                        GetErrList(ref data, ref errList);
                        if (errList.Count > 0)
                            throw new Exception(errList[0]);
                    }
                }
                else
                    throw new Exception(@"Web Service http://sp.korablik.local не вернул данных");
            }
            catch (Exception err)
            {
                ret = new Exception(err.Message, err);
            }
            return ret;
        }
        Exception GetErrList(ref byte[] parData, ref List<string> parErrList)
        {
            Exception ret = null;
            try
            {
                StringBuilder sb = new StringBuilder();
                foreach (byte bt in parData)
                    sb.Append((char)bt);
                string str = sb.ToString();
                XDocument xErrors = XDocument.Parse(str);
                var query = from g in xErrors.Descendants("Errors")
                            select new
                            {
                                errName = (string)g.Element("Err")
                            };
                foreach (var node in query)
                {
                    if (node.errName == null)
                    {
                        // Errors not found
                    }
                    else
                    {
                        parErrList.Add(node.errName);
                    }
                }
            }
            catch (Exception err)
            {
                ret = new Exception(err.Message, err);
            }
            return ret;
        }
        #endregion        

        #region Browser
        private void oBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (ApproveStep == 1)
            {
                // approve form loaded
                ApproveStep = 2;
                return;
            }
            else if (ApproveStep >= 2 && tabControl1.SelectedTab == tabBrowser && ApproveStep < 5)
            {
                ApproveStep++;
            }
            else if (ApproveStep >= 2 && tabControl1.SelectedTab != tabBrowser)
            {
                ApproveStep = 0;
                return;
            }

            if (ApproveStep >= 5)
            {
                ApproveStep = 0;
                tabControl1.SelectedTab = tabData;
                if (_selrow != null)
                {
                    _selrow.Cells["IsApprove"].Value = true;
                }
            }
        }
        #endregion       

        #region Approve form

        private void oDocStatus_SelectionChangeCommitted(object sender, EventArgs e)
        {
            btnSave.Enabled = true;
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = false;
            tabControl1.SelectedTab = tabData;
            System.Threading.Thread.Sleep(50);
            tabData.Focus();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            string newStatusValueStr = oDocStatus.Text;
            if (_selrow == null || _selrow.Index == -1)
            {
                btnSave.Enabled = false;
                tabControl1.SelectedTab = tabData;
                System.Threading.Thread.Sleep(50);
                tabData.Focus();
                return;
            }

            int id = (int)_selrow.Cells["ID"].Value;
            int iStat = oDocStatus.SelectedIndex + 1;
            DataTable tbl = new DataTable("tbl");
            List<string> errList = new List<string>();
            string param = @"&newstatus=" + iStat;
            Exception err =
                Get_ByID_And_Param(ref tbl, ref errList
                , @"http://sp.korablik.local:98/ws/SP/PermDocs_ApproveItem.aspx"
                , id, param);

            if (err == null && errList.Count == 0)
            {
                switch (iStat)
                {
                    case 1:
                        _selrow.Cells["_x0421__x043e__x0441__x0442__x043e__x044f__x043d__x0438__x0435__x0020__x0443__x0442__x0432__x0435__x0440__x0436__x0434__x0435__x043d__x0438__x044f_"].Value = "Черновик";
                        _selrow.Cells["IsApprove"].Value = false;
                        break;
                    case 2:
                        _selrow.Cells["_x0421__x043e__x0441__x0442__x043e__x044f__x043d__x0438__x0435__x0020__x0443__x0442__x0432__x0435__x0440__x0436__x0434__x0435__x043d__x0438__x044f_"].Value = "Ожидают утверждения";
                        _selrow.Cells["IsApprove"].Value = false;
                        break;
                    case 3:
                        _selrow.Cells["_x0421__x043e__x0441__x0442__x043e__x044f__x043d__x0438__x0435__x0020__x0443__x0442__x0432__x0435__x0440__x0436__x0434__x0435__x043d__x0438__x044f_"].Value = "Утвержден";
                        _selrow.Cells["IsApprove"].Value = true;
                        break;
                }
            }
            else
            {
                if (err != null)
                    MessageBox.Show(err.Message);
                if (errList.Count > 0)
                    MessageBox.Show(errList[0]);
            }

            btnSave.Enabled = false;
            tabControl1.SelectedTab = tabData;
            System.Threading.Thread.Sleep(50);
            tabData.Focus();
        }

        #endregion



    }
}
